package com.fengwk.user.controller;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fengwk.support.util.ResultUtils;
import com.fengwk.user.face.model.Permission;
import com.fengwk.user.service.PermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@com.fengwk.user.face.annotation.Permission("permission")
@Api(tags = "权限")
@RestController
@RequestMapping("/api/permission")
public class PermissionController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private PermissionService permissionService;
	
	@ApiOperation(
			value = "存储权限", 
			response = Permission.class)
	@PostMapping("/savePermission")
	public void savePermission(
			@ApiParam(value = "权限", required = true) @RequestParam(value = "permission", required = true) String permission,
			@ApiParam(value = "描述", required = false) @RequestParam(value = "description", required = false) String description) {
		ResultUtils.returnJsonResult(request, response, permissionService.savePermission(permission, description));
	}
	
	@ApiOperation(
			value = "移除权限", 
			response = Permission.class)
	@PostMapping("/removePermission")
	public void removePermission(
			@ApiParam(value = "权限id", required = true) @RequestParam(value = "permissionId", required = true) Long permissionId) {
		ResultUtils.returnJsonResult(request, response, permissionService.removePermission(permissionId));
	}
	
	@ApiOperation(
			value = "更新权限", 
			response = Permission.class)
	@PostMapping("/updatePermission")
	public void updatePermission(
			@ApiParam(value = "权限id", required = true) @RequestParam(value = "permissionId", required = true) Long permissionId,
			@ApiParam(value = "权限", required = false) @RequestParam(value = "permission", required = false) String permission,
			@ApiParam(value = "描述", required = false) @RequestParam(value = "description", required = false) String description) {
		ResultUtils.returnJsonResult(request, response, permissionService.updatePermission(permissionId, permission, description));
	}
	
	@ApiOperation(
			value = "通过用户id获取权限列表", 
			response = Permission.class)
	@PostMapping("/listPermissionByUserId")
	public void listPermissionByUserId(
			@ApiParam(value = "用户id", required = true) @RequestParam(value = "userId", required = true) Long userId) {
		ResultUtils.returnJsonResult(request, response, permissionService.listPermissionByUserId(userId));
	}
	
	@ApiOperation(
			value = "通过角色id列表获取权限列表", 
			response = Permission.class)
	@PostMapping("/listPermissionByRoleIds")
	public void listPermissionByRoleIds(
			@ApiParam(value = "角色id列表", required = true) @RequestParam(value = "roleIds", required = true) Collection<Long> roleIds) {
		ResultUtils.returnJsonResult(request, response, permissionService.listPermissionByRoleIds(roleIds));
	}
	
	@ApiOperation(
			value = "通过角色id获取权限列表", 
			response = Permission.class)
	@PostMapping("/listPermissionByRoleId")
	public void listPermissionByRoleId(
			@ApiParam(value = "角色id", required = true) @RequestParam(value = "roleId", required = true) Long roleId) {
		ResultUtils.returnJsonResult(request, response, permissionService.listPermissionByRoleId(roleId));
	}
	
	@ApiOperation(
			value = "分页获取权限", 
			response = Permission.class)
	@PostMapping("/pagePermission")
	public void pagePermission(
			@ApiParam(value = "页码", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
			@ApiParam(value = "每页大小", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize) {
		ResultUtils.returnJsonResult(request, response, permissionService.pagePermission(pageNum, pageSize));
	}
	
}
